package com.hzya.frame.web.shiro.rm;

import com.hzya.frame.system.login.service.ILoginService;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.system.user.service.IUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;


public class UserRealm extends AuthorizingRealm  {

	@Autowired
	private ILoginService loginService;
	
	@Autowired
	private IUserService userService;
	
	@Override
	//授权
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
	    //获取当前登录的用户名,等价于(String)principals.fromRealm(this.getName()).iterator().next()    
		 String userLoginName = (String)principals.getPrimaryPrincipal();
	      	UserEntity model=new UserEntity();
	      	model.setLogin_name(userLoginName);
		 try {
			 UserEntity user = userService.queryOne(model);    
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	     Set<String> roles = new HashSet<String>();
	     Set<String> permissions = new HashSet<String>();
	 
	     permissions.add("message");
	 
	     SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
	     info.setRoles(roles);
	     info.setStringPermissions(permissions);
	     return info;
	}

	@Override
	//认证
	 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException
	   {
	     UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
	     UserEntity paramDomain = new UserEntity();
	     
	     String[]strAy = token.getUsername().split("\\$");
	     paramDomain.setLogin_name(strAy[0]);
	     //paramDomain.setPlatform_account((strAy[1]));
	     paramDomain.setPassword(new String(token.getPassword()));
	     UserEntity user = this.userService.queryOne(paramDomain);
	    boolean lockFlag=false;
		try {
			//lockFlag = loginService.checkUserLock(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
	     if ((user == null))
	       throw new UnknownAccountException("account not exist");
	     if (lockFlag) {
	       throw new LockedAccountException("account locked");
	     }
	 String str=getName();
	     SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo( user.getLogin_name(), user.getPassword(),getName());
	 
	     return authenticationInfo;
	   }
	 
	   protected void clearCache(PrincipalCollection principals)
	   {
	     super.clearCache(principals);
	   }
	 
	   public void clearAllCachedAuthorizationInfo() {
	     getAuthorizationCache().clear();
	   }
	 
	   public void clearAllCachedAuthenticationInfo() {
	     getAuthenticationCache().clear();
	   }
	 
	   public void clearAllAuthCache() {
	     clearAllCachedAuthorizationInfo();
	     clearAllCachedAuthenticationInfo();
	   }
	   public static void main(String[] args) {
		String tempStr="hzhy_admin$hzhy";
		System.out.println(tempStr.split("\\$")[0]);
	}
}
